<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>消息推送表编辑--${site.name}</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="description" content="${site.description}"/>
    <meta name="keywords" content="${site.keywords}"/>
    <meta name="author" content="${site.author}"/>
    <link rel="icon" href="${site.logo}">
    <link rel="stylesheet" href="${base}/static/layui/css/layui.css" media="all"/>
    <style type="text/css">
        .layui-form-item .layui-inline {
            width: 33.333%;
            float: left;
            margin-right: 0;
        }

        @media (max-width: 1240px) {
            .layui-form-item .layui-inline {
                width: 100%;
                float: none;
            }
        }

        .layui-form-item .role-box {
            position: relative;
        }

        .layui-form-item .role-box .jq-role-inline {
            height: 100%;
            overflow: auto;
        }
        .none{
            display: none;
        }

    </style>
</head>
<body class="childrenBody">
<form class="layui-form" style="width:80%;">
    <input value="${electricityMessagePush.id}" name="id" id="id" type="hidden">
    <div class="layui-form-item" style="position: relative;z-index: 12008">
        <label class="layui-form-label">消息类型</label>
        <div class="layui-input-block">
            <select name="messageType" id="messageType" disabled="true">
                <option value="" selected="">请选择消息类型</option>
                <@my type="electricity_message_push_message_type">
                    <#list result as r>
                        <option value="${r.value}" <#if (electricityMessagePush.messageType == r.value)> selected="" </#if> >${r.label}</option>
                    </#list>
                </@my>
            </select>
        </div>
    </div>
    <div class="layui-form-item" style="position: relative;z-index: 11008">
        <label class="layui-form-label">是否微信推送</label>
        <div class="layui-input-block">
            <select name="isWechat"  id="isWechat" lay-filter="isWechat" disabled="true">
                <option value="" selected="">请选择是否微信推送</option>
                <@my type="electricity_message_push_is_wechat">
                    <#list result as r>
                        <option value="${r.value}" <#if (electricityMessagePush.isWechat == r.value)> selected="" </#if> >${r.label}</option>
                    </#list>
                </@my>
            </select>
            <input type="hidden" id="isWechatValue" value="${electricityMessagePush.isWechat}">
        </div>
    </div>

    <div class="layui-form-item wxChat" style="position: relative;z-index: 10008">
        <label class="layui-form-label">模板名称</label>
        <div class="layui-input-block">
            <select name="freemakerName" id="freemakerId" lay-filter="freemakerId">
            </select>
            <input type="hidden" id="freemakerIdValue" value="${electricityPushFreemaker.id}">
        </div>
    </div>
    <div class="layui-form-item noWxChat">
        <label class="layui-form-label">标题</label>
        <div class="layui-input-block">
            <input type="text" class="layui-input" value="${electricityMessagePush.title}" name="title" id="title"
                    placeholder="请输入标题">

        </div>
    </div>
    <div class="layui-form-item noWxChat">
        <label class="layui-form-label">内容</label>
        <div class="layui-input-block">
            <div id="content">${electricityMessagePush.content}</div>
<#--            <textarea name="content" id="content" placeholder="请输入内容" class="layui-textarea">${electricityMessagePush.content}</textarea>-->
        </div>
    </div>
    <div class="layui-form-item wxChat">
        <label class="layui-form-label">消息推送描述</label>
        <div class="layui-input-block" id="wxDesc1">
            <textarea name="wxDesc" id="wxDesc" placeholder="请输入内容" class="layui-textarea"></textarea>
            <div class="wxDesc2"></div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit="" lay-filter="addElectricityMessagePush">保存草稿</button>
            <button class="layui-btn" lay-submit="" lay-filter="sendElectricityMessagePush">立即推送</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
    <input type="hidden"  value="${descList.wxCode}">
</form>
<script type="text/javascript" src="${base}/static/layui/layui.js"></script>
<script type="text/javascript" src="${base}/static/js/wangEditor.min.js"></script>
<script type="text/javascript" src="${base}/static/js/libs/jquery-2.1.1.min.js"></script>

<script>
    layui.use(['form', 'jquery', 'layer', 'laydate'], function () {
        var form = layui.form,
            $ = layui.jquery,
            laydate = layui.laydate,
            E = window.wangEditor,
            layer = layui.layer;


        var value = $("#isWechatValue").val();
        if (value == 2) {
            $(".noWxChat").show();
            $(".wxChat").hide();
        } else {
            $(".wxChat").show();
            $(".noWxChat").hide();
        }

        form.on('select(isWechat)', function (data) {
            var value = data.value;
            if (value == 2) {
                $(".noWxChat").show();
                $(".wxChat").hide();
            } else {
                $(".wxChat").show();
                $(".noWxChat").hide();
            }
        });

        form.on("select(freemakerId)", function (data) {
            $('.wxDesc2').empty()
            var loadIndex = layer.load(2, {
                shade: [0.3, '#333']
            });
            var id = $("#freemakerIdValue").val();
            $.post("${base}/admin/electricityPushFreemakerDesc/queryMessagePushFormwordDesc", {"id": id}, function (res) {
                layer.close(loadIndex);
                if (res) {
                    if (res.length == 0){
                        $("#wxDesc1 textarea").removeClass('none');
                        $(".wxDesc2").addClass('none')
                    }else {
                        $("#wxDesc1 textarea").addClass('none');
                        $(".wxDesc2").removeClass('none')
                        for (var i = 0; i < res.length; i++) {
                            $(".wxDesc2").append(res[i].wxDesc + ':<input class="layui-input" name="' + res[i].wxDesc + '" type="text"/><input type="hidden" name="wxCode" value="' + res[i].wxCode + '" />');
                        }
                    }
                    // layui.form.render('textarea')
                }
            });
            return false;
        });

        //初始赋值
        laydate.render({
            elem: '#pushDate'
            <#if (electricityMessagePush.pushDate)??>
            , value: '${electricityMessagePush.pushDate?string("yyyy-MM-dd")}'
            </#if>
        });

        var content_editor = new E('#content');
        //图片上传
        content_editor.customConfig.uploadImgServer = '${base}/file/uploadWang';
        content_editor.customConfig.uploadFileName = 'test';
        // 自定义处理粘贴的文本内容(这里处理图片抓取)
        content_editor.customConfig.pasteTextHandle = function (content) {
            if (undefined == content) {
                return content;
            }
            if (content.indexOf("src=") <= 0) {
                return content;
            }
            var loadContent = layer.load(2, {
                shade: [0.3, '#333']
            });
            $.ajax({
                url: "${base}/file/doContent/",
                type: "POST",
                async: false,
                data: {"content": content},
                dataType: "json",
                success: function (res) {
                    layer.close(loadContent);
                    content = res.data;
                }
            });
            return content;
        };
        // 关闭粘贴样式的过滤
        content_editor.customConfig.pasteFilterStyle = false;
        content_editor.customConfig.customAlert = function (info) {
            // info 是需要提示的内容
            layer.msg(info);
        };
        content_editor.create();

        //获取消息模板
        $.ajax({
            type: 'post',
            url: "${base}/admin/electricityPushFreemakerDesc/queryMessagePushFormwork",
            contentType: "application/json; charset=utf-8",
            async: false,
            success: function (data) {
                for (var i = 0; i < data.length; i++) {
                    $("#freemakerId").append("<option value='" + data[i].stringId + "'>" + data[i].freemakerName + "</option>");
                }
                if($("#freemakerIdValue").val()!=""){
                    $("#freemakerId").find("option[value=" + $("#freemakerIdValue").val() + "]").prop("selected", true);
                }
                layui.form.render('select')
            }
        });

        var id = $('#id').val();
         $.ajax({
            type: 'post',
            url: "${base}/admin/electricityMessagePush/queryMessageDesc?id="+id,
            // data:{"id":id},
            contentType: "application/json; charset=utf-8",
            async: false,
            success: function (data) {
                if(data!=null){
                    $("#wxDesc1 textarea").addClass('none');
                    for(var i=0;i<data.length;i++){
                        var code=data[i].wxCode;
                        var value=data[i].wxDesc;;

                        if(code!=undefined&&code!=""){
                            if(value=="underfind")
                                value="";
                            $(".wxDesc2").append(data[i].wxCode+':<input class="layui-input" value="'+value+'" type="text" name="'+data[i].wxCode+'" /><input type="hidden" name="wxCode" value="'+data[i].wxCode+'" />');
                        }else{
                            $(".wxDesc2").append('<input class="layui-input" value="'+value+'" type="text"  /><input type="hidden" name="wxCode" value="'+data[i].wxCode+'" />');

                        }
                        }

                }else {

                }


                layui.form.render('select')
            }
        });

    form.on("submit(addElectricityMessagePush)", function (data) {
        var c = content_editor.txt.html();
        c = c.replace(/\"/g, "'");
        data.field.content = c;

        var inputList = $("#wxDesc1").find('input');
        console.log(inputList);
        var obj = {};
        for (var i = 0; i < inputList.length; i++) {
            var item = inputList[i];
            var name = $(item).attr('name');
            obj[name] = $(item).val();
        }
        var formdata = {};
        formdata.messageType = $("#messageType").val();
        formdata.isWechat = $("#isWechat").val();
        formdata.freemakerId = $("#freemakerId").val();
        formdata.title = $("#title").val();
        formdata.wxDesc = $("#wxDesc").val();
        formdata.content = data.field.content;
        formdata.wxList = JSON.stringify(obj);
        formdata.id = id;
        console.log(JSON.stringify(formdata));
        var loadIndex = layer.load(2, {
            shade: [0.3, '#333']
        });
        $.post("${base}/admin/electricityMessagePush/edit", {
            data: JSON.stringify(formdata),
            type: 2
        }, function (res) {

            layer.close(loadIndex);
            if (res.success) {
                parent.layer.msg("消息推送表添加成功！", {time: 1000}, function () {
                    parent.layer.close(parent.addIndex);
                    //刷新父页面
                    parent.location.reload();
                });
            } else {
                layer.msg(res.message);
            }
        });
        return false;
    });

    form.on("submit(sendElectricityMessagePush)", function (data) {
        var c = content_editor.txt.html();
        c = c.replace(/\"/g, "'");
        data.field.content = c;

        var inputList = $("#wxDesc1").find('input');
        console.log(inputList);
        var obj = {};
        for (var i = 0; i < inputList.length; i++) {
            var item = inputList[i];
            var name = $(item).attr('name');
            obj[name] = $(item).val();
        }
        var formdata = {};
        formdata.messageType = $("#messageType").val();
        formdata.isWechat = $("#isWechat").val();
        formdata.freemakerId = $("#freemakerId").val();
        formdata.title = $("#title").val();
        formdata.wxDesc = $("#wxDesc").val();
        formdata.content = data.field.content;
        formdata.wxList = JSON.stringify(obj);
        formdata.id = id;
        console.log(JSON.stringify(formdata));
        var loadIndex = layer.load(2, {
            shade: [0.3, '#333']
        });
        $.post("${base}/admin/electricityMessagePush/edit", {
            data: JSON.stringify(formdata),
            type: 1
        }, function (res) {

            layer.close(loadIndex);
            if (res.success) {
                parent.layer.msg("消息推送表添加成功！", {time: 1000}, function () {
                    parent.layer.close(parent.addIndex);
                    //刷新父页面
                    parent.location.reload();
                });
            } else {
                layer.msg(res.message);
            }
        });
        return false;
    });
    });

    function renderForm() {
        layui.use('form', function () {
            var form = layui.form
            form.render();
        });
    }
</script>
</body>
</html>